package com.ruoyi.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@Configuration
public class RabbitMQConfig {

    public static final String ORDER_EXCHANGE = "order.exchange";
    public static final String ORDER_QUEUE = "order.queue";
    public static final String ORDER_ROUTING_KEY = "order.routing.key";

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // fanout交换机
    @Bean("fex1")
    public Exchange getExchange1() {
        return ExchangeBuilder.fanoutExchange("fex1").durable(true).build();
    }

    // 死信交换机
    @Bean("fex2")
    public Exchange getExchange2() {
        return ExchangeBuilder.fanoutExchange("fex2").durable(true).build();
    }
    //队列
    @Bean("q1")
    public Queue getQueue1() {
        return QueueBuilder.durable("q1").build();
    }

    // 延时队列
    @Bean("yanshiQueue")
    public Queue yanshiQueue() {
        return QueueBuilder.durable("yanshiQueue")
                // 设置消息过期时间为 10 秒
                .ttl(10000)
                // 设置死信交换机, 路由键为空 即超过时间后会将消息转发到死信交换机
                .deadLetterExchange("fex2")
                .deadLetterRoutingKey("")
                .build();
    }

    // 死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlxQueue")
                .build();
    }

    @Bean
    public Binding getBinding1(Exchange fex1, Queue q1) {
        return BindingBuilder.bind(q1).to(fex1).with("").noargs();
    }

    // 绑定延时队列到延时交换机
    @Bean
    public Binding getBinding2(Exchange fex1, Queue yanshiQueue) {
        return BindingBuilder.bind(yanshiQueue).to(fex1).with("").noargs();
    }

    // 绑定死信队列到死信交换机
    @Bean
    public Binding getBinding3(Exchange fex2, Queue dlxQueue) {
        return BindingBuilder.bind(dlxQueue).to(fex2).with("").noargs();
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // 设置手动确认模式
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        return factory;
    }
}
